package org.sapmentors.returns.material;

import java.util.ArrayList;

import java.io.*;
import java.net.*;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.sapmentors.WebserviceHelper;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class QueryMaterial extends DefaultHandler {
  String materialNumber ;
	public QueryMaterial() {
	
	}
	public ArrayList<Material> getMaterial(String customerId){
		 materialList = new ArrayList<Material>();
		
		 try {
			 
			 HttpURLConnection connection=  WebserviceHelper.getConnection("http://erp.esworkplace.sap.com:80/sap/bc/srt/xip/sap/ECC_INDMATERPSIMPBYELEV1QR?sap-client=800",
					 getMessage(customerId));
	            
	            
	            StringBuffer sb = new StringBuffer();
	            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

	            	SAXParserFactory factory = SAXParserFactory.newInstance();
	                SAXParser saxParser = factory.newSAXParser();
	                materialList = new ArrayList<Material>();
	                
	                saxParser.parse(connection.getInputStream(), this);


	            } else {
	            	materialList = new ArrayList<Material>();
		           return materialList ;
		            // Server returned HTTP error code.
	            }
	            
	        } catch (Exception e){
	        	System.out.println("Exception x");
	        	e.printStackTrace();
	        }

	        return materialList;
		
	}
	
	String name ;
    Material material = new Material();
    ArrayList<Material> materialList;
    
    
    
    
    public void startElement(String uri, String localName,
        String qName, Attributes attributes)
        throws SAXException {
        this.name = qName;
    }

    public void characters(char ch[], int start, int length)
        throws SAXException {
    	String value = new String(ch, start, length);
        if(name.equals("ID")){
        	material.setId(value);
        }else if(name.equals("MaterialID")){
        	material.setMaterialId(value);
        }else if (name.equals("Description")){
        	material.setDescription(value);
        }
     
    }
    
    public void endElement(String uri, String localName,
    		String name) throws SAXException {
       
       if(name.equals("IndividualMaterial")){
    	   materialList.add(material);
    	   material = new Material();
       }
    }

	
	/**
	 * 
	 * @return String message - the original xml message
	 */
	private String getMessage(String customerId){
		return "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:glob=\"http://sap.com/xi/EA-APPL/SE/Global\">"
  +"   <soapenv:Header/>"
  +"<soapenv:Body><glob:IndividualMaterialERPSimpleByElementsQuery_sync_V1><IndividualMaterialSimpleSelectionByElements>"
  +"          <CustomerID>"+customerId+"</CustomerID>"
  +"      </IndividualMaterialSimpleSelectionByElements> </glob:IndividualMaterialERPSimpleByElementsQuery_sync_V1>"
  +"</soapenv:Body>"
  +"</soapenv:Envelope>";
	}

}
